<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">公司名称</label>
        <el-input v-model="query.companyName" clearable placeholder="公司名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
        :title="crud.status.title" width="800px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
          <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称">
              <el-input v-model="form.name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司地址">
              <el-input v-model="form.address" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册地">
              <el-input v-model="form.domicile" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法定代表人">
              <el-input v-model="form.legal" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人固定电话">
              <el-input v-model="form.legalTelephone" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人手机号码">
              <el-input v-model="form.legalMobile" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="法人电子邮箱">
              <el-input v-model="form.legalEmail" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成立日期">
              <el-input v-model="form.establishDate" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司背景信息">
              <el-input v-model="form.companyInfo" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="财务信息">
              <el-input v-model="form.financialInfo" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行业分类">
              <el-input v-model="form.industryClass" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业范围">
              <el-input v-model="form.businessScope" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营状态" prop="status">
              <el-select v-model="form.status" filterable placeholder="请选择">
                <el-option v-for="item in dict.Company_status" :key="item.id" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司规模">
              <el-input v-model="form.companySize" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- <el-form-item label="上级部门">
              <el-input v-model="form.pid" />
            </el-form-item> -->
            <el-form-item label="上级部门" prop="'pdept.id'">
              <treeselect
                  v-model="form.pid"
                  :options="depts"
                  :load-options="loadDepts"
                  style="width: 178px"
                  placeholder="选择部门"
                />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="子部门数目">
              <el-input v-model="form.subCount" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="enabled">
              <el-radio v-model="form.enabled" v-for="item in dict.dept_status" :key="item.id" :label="item.value">{{
                item.label }}</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
        @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="deptSort" label="排序" />
        <el-table-column prop="name" label="公司名称" />
        <el-table-column prop="address" label="公司地址" />
        <el-table-column prop="domicile" label="注册地" />
        <el-table-column prop="legal" label="法定代表人" />
        <el-table-column prop="legalTelephone" label="法人电话" />
        <el-table-column prop="legalMobile" label="法人手机" />
        <el-table-column prop="legalEmail" label="法人邮箱" />
        <el-table-column prop="establishDate" label="成立日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.establishDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="establishDate" label="创建日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="establishDate" label="修改日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="companyInfo" label="公司信息" />
        <el-table-column prop="financialInfo" label="财务信息" />
        <el-table-column prop="industryClass" label="行业分类" />
        <el-table-column prop="businessScope" label="营业范围" />
        <el-table-column prop="status" label="经营状态">
          <template slot-scope="scope">
            {{ dict.label.Company_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="companySize" label="公司规模" />
        <el-table-column prop="pdeptName" label="上级部门" />
        <el-table-column prop="subCount" label="子部门数目" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="enabled" label="启用状态">
          <template slot-scope="scope">
            {{ dict.label.dept_status[scope.row.enabled] }}
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="创建者" />
        <el-table-column prop="updatedBy" label="更新者" />
        <el-table-column fixed="right" v-permission="['admin', 'sysDept:edit', 'sysDept:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { getDepts, getDeptsAll } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'

import crudSysDept from '@/api/system/dept'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
const defaultForm = { deptId: null, pid: null, pdept: { id: null, name: '测试部' }, subCount: null, name: null, deptSort: null, enabled: 'true', createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'SysDept',
  dicts: ['dept_status', 'Company_status'],
  components: { Treeselect, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '单位', url: 'api/dept', idField: 'id', sort: 'deptSort,desc', crudMethod: { ...crudSysDept }})
  },
  data() {
    return {
      depts: [],
      permission: {
        add: ['admin', 'sysDept:add'],
        edit: ['admin', 'sysDept:edit'],
        del: ['admin', 'sysDept:del']
      },
      rules: {
        status: [
          { required: true, message: '企业经营状态', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '公司名称不能为空', trigger: 'blur' }
        ],
        enabled: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'companyName', display_name: '公司名称' },
        { key: 'status', display_name: '企业经营状态' },
        { key: 'enabled', display_name: '状态' }
      ],
    }
  },
  methods: {
    // 获取弹窗内部门数据
    loadDepts({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        getDepts({ enabled: true, pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 200)
        })
      }
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      this.getDeptsList()      
    },
    getDeptsList() {
      getDeptsAll({ enabled: true }).then(res => {
        this.depts = res.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>
<style scoped></style>
